package com.wushijia.classload;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

//类文件是使用javap来查看
public class ClassTest {

  public static void main(String[] args) {

    ClassTest classTest = new ClassTest();
    Class<?> clazz = classTest.getClass();
    System.out.println(Void.class == Void.TYPE);
    //基本类型的类字面量和类对象一样
    System.out.println(int.class == Integer.TYPE);
    System.out.println(String.class == "".getClass());
    System.out.println(clazz);
    System.out.println(commonAncestor(ChildClassTest1.class, ChildClassTest2.class));
  }

  @Deprecated
  public void dep() {
    System.out.println("Deprecated method");
  }

  //两个类的共同父类
  public static Class<?> commonAncestor(Class<?> class1, Class<?> class2) {
    if (class1 == null || class2 == null) {
      return null;
    }
    if (class1.equals(class2)) {
      return class1;
    }
    if (class1.isPrimitive() || class2.isPrimitive()) {
      return null;
    }
    List<Class<?>> classList = new ArrayList<>();
    Class<?> c = class1;
    while (!c.equals(Object.class)) {
      if (c.equals(class2)) {
        return c;
      }
      classList.add(c);
      c = c.getSuperclass();
    }

    c = class2;
    while (!c.equals(Object.class)) {
      if (c.equals(class1)) {
        return c;
      }
      if (classList.contains(c)) {
        return c;
      }

      c = c.getSuperclass();
    }

    return Object.class;
  }

  static class ChildClassTest1 extends ClassTest {

  }
  static class ChildClassTest2 extends ClassTest {

  }
}
